package io.hxci.modules.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import io.hxci.common.annotation.DataFilter;
import io.hxci.common.utils.Constant;
import io.hxci.common.utils.PageUtils;
import io.hxci.common.utils.Query;
import io.hxci.modules.edu.dao.EduRecordStuMapper;
import io.hxci.modules.edu.dao.EduStudentMapper;
import io.hxci.modules.edu.domain.*;
import io.hxci.modules.edu.service.EduClassService;
import io.hxci.modules.edu.service.EduRecordStuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.hxci.modules.edu.dao.EduRecordMapper;
import io.hxci.modules.edu.service.EduRecordService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EduRecordServiceImpl extends ServiceImpl<EduRecordMapper, EduRecord> implements EduRecordService{
    @Autowired
    EduRecordMapper mapper;
    @Autowired
    private EduClassService eduClassService;
    @Autowired
    EduRecordStuService stuService;
    @Autowired
    EduStudentMapper smapper;

    @Transactional
    public String batchAdd(EduRecord record, Long[] bjs) {
        String bjids ="";
        for (Long bj:bjs) {
            bjids = bjids + bj+"-";
        }
        bjids = bjids.substring(0,bjids.length()-1);


        EduRecord eduRecord = mapper.selectOne(
                new QueryWrapper<EduRecord>().
                        eq("tid",record.getTid()).
                        eq("sksj",record.getSksj()).eq("bnames",bjids).
                        apply("date_format(createtime,'%Y-%m-%d' )  =date_format(now(),'%Y-%m-%d' )"));















        if(eduRecord == null){
            String recordid = UUID.randomUUID().toString();
            record.setId(recordid);
            record.setTimer(System.currentTimeMillis()+"");
            record.setBnames(bjids);

            mapper.insert(record);

            List<EduStudent> eduStudents = smapper.selectList(new QueryWrapper<EduStudent>().in("newbj",bjs));
            List<EduRecordStu> eduRecordStus = Lists.newArrayList();
            eduRecordStus = eduStudents.stream().map(item ->{
                EduRecordStu eduRecordStu =  new EduRecordStu();
                String id = UUID.randomUUID().toString();
                eduRecordStu.setId(id);
                eduRecordStu.setSid(item.getId());
                eduRecordStu.setBid(item.getNewbj());
                eduRecordStu.setRecordid(recordid);

                return eduRecordStu;
            }).collect(Collectors.toList());

            stuService.saveBatch(eduRecordStus);
            return recordid;
        }else{
            return eduRecord.getId();
        }




    }

    @Override
    public PageUtils record(Map<String, Object> params, Long userId) {
        IPage<EduRecord> page = new Query<EduRecord>().getPage(params);
       List<EduRecord> list =this.baseMapper.record(page,userId);

        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void delete(String[] id) {

        for (String i:id ) {
            mapper.deleteById(i);
            stuService.remove(new QueryWrapper<EduRecordStu>().eq("recordid",id));
        }
    }
}
